define([
    'base',
    'bootstrapDialog',
    'bootstrapTable',
    'jqViewer',
    'jqPrint',
    "css!global/plugins/viewer/viewer.min.css"
], function (base, BootstrapDialog) {
    //默认加载基础库
    base();

    return {
        list: function () {
            var myTable = $('#dataList');
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url: 'api.json'})
            };
            //table按需刷新动作
            var doAction = function (obj) {
                myTable.bootstrapTable('refresh', {url: 'api.json?pid=' + obj.id})
            };

            //table当前行的响应事件
            window.operateEvents = {
                'click .info': function (e, value, row) {
                    BootstrapDialog.show({
                        type: BootstrapDialog.TYPE_DEFAULT,
                        size: BootstrapDialog.SIZE_WIDE,
                        tabindex: null,
                        title: row.itemName + " - 进度详情",
                        closeByBackdrop: false,
                        message: $('<div></div>').load('../view/' + row.itemType + '/' + row.itemId)
                    });
                }
            };

            //table表格
            myTable.bootstrapTable({
                url: 'api.json', // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [
                    {
                        field: 'termLevel',
                        title: '预警',
                        align: 'center',
                        width: '5%',
                        formatter: function (value, row, index) {
                            var result = "<i class=\"fa fa-2x fa-smile-o text-primary\" aria-hidden=\"true\"></i>";
                            switch (value) {
                                case 1:
                                    result = "<i class=\"fa fa-2x fa-smile-o text-primary\" aria-hidden=\"true\"></i>";
                                    break;
                                case 2:
                                    result = "<i class=\"fa fa-2x fa-meh-o text-warning\" aria-hidden=\"true\"></i>";
                                    break;
                                case 3:
                                    result = "<i class=\"fa fa-2x fa-frown-o text-danger\" aria-hidden=\"true\"></i>";
                                    break;
                            }
                            return result;
                        }
                    }, {
                        field: 'itemName',
                        title: '事项信息',
                        width: '26%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-hashtag text-primary" aria-hidden="true"></i>&emsp;<b>' + value + '</b>';
                        }
                    },
                    {
                        field: 'userName',
                        title: '申请信息',
                        width: '13%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + value + '<br /><i class="fa fa-clock-o" aria-hidden="true"></i>&emsp;' + row.createTime
                        }
                    }, {
                        field: 'auditorUserName',
                        title: '审核信息',
                        width: '13%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + value + '<br /><i class="fa fa-clock-o" aria-hidden="true"></i>&emsp;' + row.updateTime
                        }
                    }, {
                        field: 'promiseTerm',
                        title: '时效信息',
                        width: '13%',
                        formatter: function (value, row, index) {
                            console.log(row);
                            return '<i class="fa fa-calendar-check-o" aria-hidden="true"></i>&emsp;'+value+'个工作日<br /><i class="fa fa-clock-o" aria-hidden="true"></i>&emsp;' + row.deadline
                        }
                    }, {
                        field: 'complete',
                        title: '处理进度',
                        align: 'center',
                        width: '10%',
                        //申请状态（0未提交暂存；1待审核；2审核中；3审核拒绝被退回；4审核通过；8放弃作废)
                        formatter: function (value) {
                            if (value === 0) {
                                return "<span class=\"label label-warning\"><i class=\"fa fa-cog\" aria-hidden=\"true\"></i>&nbsp;进行中</span>";

                            } else {
                                return "<span class=\"label label-primary\"><i class=\"fa fa-check\" aria-hidden=\"true\"></i>已完成</span>";
                            }
                        }
                    },
                    {
                        field: 'action',
                        title: '操作',
                        align: 'center',
                        width: '10%',
                        formatter: "<button type=\"button\" class=\"info btn btn-white\"><i class=\"fa fa-info-circle\"></i>&nbsp;详情&nbsp;</button>",
                        events: operateEvents
                    }
                ]
            });

            //搜索刷新
            $('#search').on('click', function () {
                tableRefresh();
            });


            //搜索参数传递
            function queryParams(params) {
                params.search = $("#searchValue").val() === '' ? null : $("#searchValue").val();
                params.complete = $("#complete").val() === '' ? null : $("#complete").val();
                params.itemType = $("#itemType").val() === '' ? null : $("#itemType").val();
                return {
                    limit: params.limit,
                    offset: params.offset,
                    complete: params.complete,
                    itemType: params.itemType,
                    search: params.search
                };
            }

            //回显
            function responseHandler(res) {
                return res.rows;
            }

        },
        view: function () {

            $("input[name='auditSelected']").on('click', function () {
                if ($("input[name='auditSelected']:checked")) {

                    //字号判断
                    if ($("#allBrandDisabled").is(':checked')) {
                        $.each($(".companyBrandList").find("input[name='auditSelected']"), function (k, v) {
                            $(v).attr("checked", false);
                            $(v).attr("disabled", "disabled");
                        })
                    } else {
                        $.each($(".companyBrandList").find("input[name='auditSelected']"), function (k, v) {
                            $(v).removeAttr("disabled");
                        })
                    }

                    //股东判断
                    if ($("#holderPictureDisabled").is(':checked')) {
                        $.each($("#sharehoderList").find(".holderPicture > input[name='auditSelected']"), function (k, v) {
                            $(v).attr("checked", false);
                            $(v).attr("disabled", "disabled");
                        })
                    } else {
                        $.each($("#sharehoderList").find(".holderPicture > input[name='auditSelected']"), function (k, v) {
                            $(v).removeAttr("disabled");
                        })
                    }

                    if ($("#holderInfoDisabled").is(':checked')) {
                        $.each($("#sharehoderList").find(".holderInfo > input[name='auditSelected']"), function (k, v) {
                            $(v).attr("checked", false);
                            $(v).attr("disabled", "disabled");
                        })
                    } else {
                        $.each($("#sharehoderList").find(".holderInfo > input[name='auditSelected']"), function (k, v) {
                            $(v).removeAttr("disabled");
                        })
                    }


                    //成员判断
                    if ($("#memberPictureDisabled").is(':checked')) {
                        $.each($("#memberList").find(".memberPicture > input[name='auditSelected']"), function (k, v) {
                            $(v).attr("checked", false);
                            $(v).attr("disabled", "disabled");
                        })
                    } else {
                        $.each($("#memberList").find(".memberPicture > input[name='auditSelected']"), function (k, v) {
                            $(v).removeAttr("disabled");
                        })
                    }

                    if ($("#memberInfoDisabled").is(':checked')) {
                        $.each($("#memberList").find(".memberInfo > input[name='auditSelected']"), function (k, v) {
                            $(v).attr("checked", false);
                            $(v).attr("disabled", "disabled");
                        })
                    } else {
                        $.each($("#memberList").find(".memberInfo > input[name='auditSelected']"), function (k, v) {
                            $(v).removeAttr("disabled");
                        })
                    }


                    //删除已选信息
                    clearRefuseMessage();

                    var auditSelected = $("#tabs-container").find("input[name='auditSelected']:checked");
                    $.each(auditSelected, function (k, v) {
                        setRefuseMessage(k, $(v).data("message") + "\n");
                    });
                    $("#auditMessage").val(getRefuseMessage());
                }
            });

            /**
             * 经营范围编辑开关
             */
            $("#changeBusinessScope").click(function () {
                if ($(this).is(":checked")) {
                    $("#businessScope").removeAttr("disabled");
                }
                else {
                    $("#businessScope").attr("disabled", "disabled");
                }
            });

            /**
             * 提交经营范围
             */
            $("#doSubmitBusinessScope").on('click', function () {
                $.baseUtils.ajax({
                    url: '../save/action',
                    text: '经营范围',
                    data: {
                        id: $("#id").val(),
                        businessScope: $("#businessScope").val()
                    }
                });
            });


            //协议证照
            $('#businessScopeImage').viewer({
                navbar: false,
                title: false
            });
            //身份证
            $('.IDcard').viewer({
                navbar: false,
                title: false
            });

            $("#doSubmit").on('click', function () {
                    var data = {
                        id: $("#id").val(),
                        auditStatus: $("input[name='auditStatus']:checked").val(),
                        auditMessage: $("#auditMessage").val()
                    };
                    $.ajax({
                        url: "/companyApply/update/flow",
                        type: 'POST',
                        data: data,
                        dataType: "JSON",
                        beforeSend: function () {
                            //触发ajax请求开始时执行
                            $("#doSubmit").text('提交中...');
                            $("#doSubmit").attr('disabled', 'disabled');
                        },
                        success: function (data) {
                            if (data.success) {
                                window.location.href = "/companyApply/view/index";
                            } else {
                                msgTool.error(data.msg);
                            }
                        },
                        error: function () {
                            msgTool.error("操作失败，请刷新当前页面后重试！");
                        },
                        complete: function () {
                            $("#doSubmit").text("提交审核");
                            $("#doSubmit").removeAttr('disabled');
                        }
                    });
                }
            );

        },

    };
});






